package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    /**
     * 发布评论
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map map) {
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentsService.saveComments(movementId, comment);
        return ResponseEntity.ok(null);
    }

    //分页查询评理列表
    @GetMapping
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String movementId) {
        PageResult pr = commentsService.findComments(movementId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    //评论点赞  王东阳
    @GetMapping("/{id}/like")
    public ResponseEntity<Integer> like(@PathVariable("id") String commentId) {
        Integer likeCount = commentsService.like(commentId);
        if (likeCount != null) {
            return ResponseEntity.ok(likeCount);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 说明:  动态评论点赞-取消
     *
     * @param commentId 评论id
     * @return org.springframework.http.ResponseEntity<java.lang.Integer>
     * @author 李磊
     * @since 2022-01-18 10:22
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Integer> unlike(@PathVariable("id") String commentId) {
        Integer likeCount = commentsService.dislike(commentId);
        if (likeCount != null) {
            return ResponseEntity.ok(likeCount);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
